<?php
class v1_member{
	function index(){
		$data['basic']=$this->yum->config['Manage']['domain'].'?c=member&a=index';
		$params=array();
		if($this->yum->get('date',true)){
			$data['date']=explode(',',$this->yum->get('date',true));
			if(count($data['date'])==2){
				$params['where']=array('C.date >='=>strtotime($data['date'][0]),'C.date <='=>strtotime($data['date'][1]));
				$data['basic'].='&date='.$data['date'][0].','.$data['date'][1];
			}
		}
		if($this->yum->get('locked',true)){
			$temp=$this->yum->get('locked',true);
			if(is_array($temp)){
				foreach($temp as $key=>$value){
					$data['locked'][$value]=intval($value);
					$data['basic'].='&locked[]='.$value;
				}
				$params['where']['A.locked IN']=$data['locked'];
			}
		}
		if($this->yum->get('role',true)){
			$temp=$this->yum->get('role',true);
			if(is_array($temp)){
				foreach($temp as $key=>$value){
					$data['role'][$value]=intval($value);
					$data['basic'].='&role[]='.$value;
				}
				if(isset($data['role'][0])){
					$temp=array();
					if(!isset($data['role'][1])){$temp[]=1;}
					if(!isset($data['role'][2])){$temp[]=2;}
					if(!empty($temp)){$params['where']['A.role NOT IN']=$temp;}
				}else{
					$params['where']['A.role IN']=$data['role'];
				}
			}
		}
		if($this->yum->get('search',true)){
			$data['search']=urldecode($this->yum->get('search',true));$data['tab']=$this->yum->get('tab',true);
			if($data['search']!=''){
				if($data['tab']=='user'){
					$params['where']['A.user LIKE']='%'.$data['search'].'%';
				}
				$data['basic'].='&tab='.$data['tab'];
				$data['basic'].='&search='.urlencode($data['search']);
			}
		}
		$member=$this->yum->model('member');
		$data['total']=$member->GMember(array('select'=>'COUNT(*) AS count','where'=>$params['where']));
		$data['total']=empty($data['total'])?0:$data['total'][0]['count'];
		$params['limit']=array(intval($this->yum->get('start')),15);
		$role=$this->yum->model('role');
		$data['role']=$role->GRole(array('where'=>array('locked='=>0)));
		$data['member']=$member->GMember($params);
		$pagination=$this->yum->library('pagination',array('query_string'=>'start','base_url'=>$data['basic'].'&order='.$data['order'].'&rule='.$data['rule'],'total_rows'=>$data['total'],'per_page'=>15,'first_link'=>'首页','last_link'=>'末页','cur_tag_open'=>'<a href="#" class="active">','cur_tag_close'=>'</a>'));
		$data['pagination']=$pagination->create_links();
		$this->yum->set($data);
		$this->yum->view('index.php');
	}
	function edit(){
		$member=$this->yum->model('member');
		if($this->yum->post('save')){
			$output=array('result'=>0,'msg'=>'');
			$id=intval($this->yum->post('member'));$role=intval($this->yum->post('role'));
			$user=$this->yum->post('user',true);$password=$this->yum->post('password',true);
			$affected=$member->UMember(array('where'=>array('id='=>$id),'set'=>array('user='=>$user,'password='=>$password,'role='=>$role)));
			if($affected==1){
				$output['result']=2;
				$output['msg']='修改成功.';
			}else{
				$output['msg']='请误提交相同或未经修改的数据.';
			}
			header('Content-Type:text/plain');
			echo json_encode($output);
		}else{
			$role=$this->yum->model('role');
			$data['role']=$role->GRole(array('where'=>array('locked ='=>0)));
			$params['where']=array('A.id ='=>intval($this->yum->get('member',true)));
			$data['member']=$member->GMember($params);
			$data['member']=reset($data['member']);
			$this->yum->set($data);
			$this->yum->view('edit.php');
		}
	}
	function locked(){
		$output=array('result'=>0,'msg'=>'');
		$member=$this->yum->model('member');
		$id=$this->yum->get('member');
		$locked=intval($this->yum->get('locked',true));
		if(is_array($id)){
			$id=array_map('intval',$id);
			if(array_search(1,$id)!==false){
				$output['msg']='超级管理员不可修改.';
			}else{
				$affected=$member->UMember(array('where'=>array('id IN'=>$id),'set'=>array('locked='=>$locked)));
				if($affected==0){
					$output['msg']='重复的数据提交.';
				}else{
					$output['result']=2;
				}
			}
		}else{
			if(intval($id)==1){
				$output['msg']='超级管理员不可删除.';
			}else{
				$affected=$member->UMember(array('where'=>array('id='=>$id),'set'=>array('locked='=>$locked)));
				if($affected==1){
					$output['result']=5;
					$output['td']=array('offset'=>6,'data'=>array($locked==0?'正常':'禁止'));
					$output['exec']=array('attr'=>array('mixed'=>'sub:{locked:'.($locked==0?2:0).'}'),'text'=>($locked==0?'禁止':'激活'));
				}else{
					$output['msg']='成员状态已变更.';
				}
			}
		}
		header('Content-Type:text/plain');
		echo json_encode($output);
	}
}
?>